/**
 * 
 */
package vue.popup;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;


import model.solver.RSatParameter;

/**
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGRSatDialogParameter extends IGDialogParameter {

	private static final long	serialVersionUID			= 1L;

	public static final String	PRINT_SOLUTION_DESCRIPTION	= "Print the solution: ";
	// public static final String TIMEOUT_BUTTON = "Set timeout";


	public static final String	YES							= "YES";
	public static final String	NO							= "NO";


	private RSatParameter		param;

	// private JButton timeoutButton;
	private JPanel				timeoutPanel;
	private JLabel				timeoutDescritpion;

	private JPanel				printSolutionPanel;
	private JLabel				printSolutionDescritpion;
	private JComboBox			printSolutionBox;


	public IGRSatDialogParameter(RSatParameter param) {
		// TODO Auto-generated constructor stub
		this.param = param;

		setLayout(new GridLayout(0, 1));

		// timeout
		timeoutPanel = new JPanel(new GridLayout(1, 2));

		// timeoutButton = new JButton(TIMEOUT_BUTTON);
		// timeoutButton.addActionListener(this);
		timeoutDescritpion = new JLabel(TIMEOUT_DESCRIPTION);
		timeoutPanel.add(timeoutDescritpion);
		timeoutPanel.add(timeoutField);
		// panel.add(timeoutButton);
		// panel.setBorder(BorderFactory.createTitledBorder(TIMEOUT_DESCRIPTION));

		add(timeoutPanel);

		// print solution
		printSolutionPanel = new JPanel(new GridLayout(1, 2));
		printSolutionDescritpion = new JLabel(PRINT_SOLUTION_DESCRIPTION);
		printSolutionBox = new JComboBox();
		printSolutionBox.addItem(YES);
		printSolutionBox.addItem(NO);

		printSolutionPanel.add(printSolutionDescritpion);
		printSolutionPanel.add(printSolutionBox);

		add(printSolutionPanel);

		refresh();
	}

	@Override
	public void refresh() {
		if (param.isPrintSolution())
			printSolutionBox.setSelectedItem(YES);
		else
			printSolutionBox.setSelectedItem(NO);

		timeoutField.setText("" + param.getTimeOut());
	}

	@Override
	public boolean checkAndSetParameter() {
		// TODO A tester
		boolean good = true;

		// timeout
		String timeout = timeoutField.getText();
		if (timeout.isEmpty())
			param.setTimeOut(RSatParameter.NO_DELAY);
		else
			try
			{
				param.setTimeOut(Integer.parseInt(timeout));
			} catch (Exception e)
			{
				timeoutPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}

		// print
		if (printSolutionBox.getSelectedItem().equals(YES))
			param.setPrintSolution(true);
		else
			param.setPrintSolution(false);


		return good;
	}

}
